package com.whj.controller;

import com.whj.domain.dto.AddRoleDTO;
import com.whj.domain.dto.UserDTO;
import com.whj.domain.entity.Role;
import com.whj.domain.entity.User;
import com.whj.domain.enums.AppHttpCodeEnum;
import com.whj.domain.response.ResponseResult;
import com.whj.domain.vo.RoleVo;
import com.whj.service.RoleService;
import com.whj.service.UserService;
import com.whj.utils.BeanCopyUtils;
import com.whj.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Auther: wanghaijun
 * @Date: 2023/6/13 - 06 - 13 - 19:54 
 * @Description: com.whj.controller
 */

@RestController
@RequestMapping("/system")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @GetMapping("/role/list")
    public ResponseResult selectRole(Integer pageNum, Integer pageSize, String roleName, String status) {
        return roleService.selectRole(pageNum, pageSize, roleName, status);
    }

    @PutMapping("/role/changeStatus")
    public ResponseResult roleChangeStatus(@RequestBody RoleVo roleVo) {
        return roleService.roleChangeStatus(roleVo);
    }

    /**
     * 新增角色
     *
     * @param addRoleDTO
     * @return
     */
    @PostMapping("/role")
    public ResponseResult addRole(@RequestBody AddRoleDTO addRoleDTO) {
        return roleService.addRole(addRoleDTO);
    }

    /**
     * 通过id 查询 role
     *
     * @param id
     * @return
     */
    @GetMapping("role/{id}")
    public ResponseResult selectRoleById(@PathVariable("id") Long id) {
        return roleService.selectRoleById(id);
    }

    @PutMapping("/role")
    public ResponseResult updateRole(@RequestBody AddRoleDTO addRoleDTO) {
        return roleService.updateRole(addRoleDTO);
    }

    @DeleteMapping("/role/{id}")
    public ResponseResult deleteRole(@PathVariable("id") Long id) {
        if (SecurityUtils.isAdmin()){
            roleService.removeById(id);
            return ResponseResult.okResult();
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.NOT_ALLOW,AppHttpCodeEnum.NOT_ALLOW.getMsg());
    }

    /**
     * 查询的是所有状态正常的角色
     *
     * @return
     */
    @GetMapping("/role/listAllRole")
    public ResponseResult listAllRole() {
        List<Role> roleList = roleService.listAllRole();
        return ResponseResult.okResult(roleList);
    }

    @Autowired
    private UserService userService;

    /**
     * 用户查询
     *
     * @param pageNum
     * @param pageSize
     * @param username 名称
     * @param status
     * @return
     */
    @GetMapping("/user/list")
    public ResponseResult listRole(Integer pageNum, Integer pageSize, String phonenumber, String username, String status) {

        return userService.listRole(pageNum, pageSize, phonenumber, username, status);
    }

    /**
     * 添加用户
     *
     * @param userDTO
     * @return
     */
    @PostMapping("/user")
    public ResponseResult addUser(@RequestBody UserDTO userDTO) {
        return userService.addUser(userDTO);
    }


    /**
     * 删除用户
     *
     * @param id
     * @return
     */
    @DeleteMapping("/user/{id}")
    public ResponseResult deleteUser(@PathVariable("id") List<String> id) {
        if (SecurityUtils.isAdmin()){
            userService.deleteUser(id);
            return ResponseResult.okResult();
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.NOT_ALLOW,AppHttpCodeEnum.NOT_ALLOW.getMsg());
    }

    @GetMapping("/user/{id}")
    public ResponseResult selectUser(@PathVariable("id") Long id) {
        return userService.selectUser(id);
    }

    @PutMapping("/user")
    public ResponseResult updateUser(@RequestBody UserDTO userDTO) {
        User user = BeanCopyUtils.copyBean(userDTO, User.class);
        return userService.updateUserInfo(user);
    }

    @PutMapping("/user/changeStatus")
    public ResponseResult changerStatus(@RequestBody RoleVo roleVo){
        return userService.changeStatus(roleVo);
    }
}
